package org.csu.myPetStore.web.servlet.Cart;

import org.csu.myPetStore.domain.Account;
import org.csu.myPetStore.domain.Cart;
import org.csu.myPetStore.domain.CartItem;
import org.csu.myPetStore.domain.Item;
import org.csu.myPetStore.service.CartService;
import org.csu.myPetStore.service.CatalogService;
import org.csu.myPetStore.service.LogcatService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Date;

public class AddItemToCartServlet extends HttpServlet {

    //1. 处理完请求后的跳转页面
    private static final String VIEW_CART = "/WEB-INF/jsp/cart/Cart.jsp";
    private static final String LOGIN = "/WEB-INF/jsp/account/SignonForm.jsp";

    //2. 定义处理该请求所需要的数据
    private String workingItemId;
    private Cart cart;

    //3. 是否需要调用业务逻辑层
    private CatalogService catalogService;


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        workingItemId = request.getParameter("workingItemId");
        CartService service = new CartService();
        LogcatService logcatService = new LogcatService();

        HttpSession session = request.getSession();
        cart = (Cart)session.getAttribute("cart");

        Account account = (Account) session.getAttribute("account");

        if(account == null)
            request.getRequestDispatcher(LOGIN).forward(request,response);
        else {

            if (cart == null) {
                cart = new Cart();
            }

            if (cart.containsItemId(workingItemId)) {
                cart.incrementQuantityByItemId(workingItemId);
                int num = service.getQuantityByItemid(workingItemId,account.getUsername());
                service.deleteCartItemFromUserByItemId(workingItemId,account.getUsername());
                service.insertCartItemByIremId(workingItemId,num+1,account.getUsername());
            } else {
                catalogService = new CatalogService();
                if(catalogService == null)
                    System.out.println("www");

                //插入item入Cart数据库
                service.insertCartItemByIremId(workingItemId,1,account.getUsername());

                System.out.println(workingItemId);

                boolean isInStock = catalogService.isItemInStock(workingItemId);
                Item item = catalogService.getItem(workingItemId);

                if(item == null)
                    System.out.println("zzz");

                java.util.Date date = new java.util.Date();
                Date datesql = new Date(date.getTime());
                logcatService.InsertLogCat(account.getUsername(),"Add" + item.getProduct().getName() + "to cast at " + datesql.toString());

                cart.addItem(item,isInStock);
            }
            session.setAttribute("cart", cart);
            request.getRequestDispatcher(VIEW_CART).forward(request, response);
        }
    }
}